package com.nbjtjc.safe.aspect;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.nbjtjc.common.exception.ValidateException;
import com.nbjtjc.safe.activiti.mapper.ExtActTaskReassignMapper;
import com.nbjtjc.safe.activiti.model.ActivitiParams;
import com.nbjtjc.safe.activiti.model.ExtActTaskReassign;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @auther guqianbin
 * @date 2018/7/23 8:29
 **/
@Aspect
@Component
public class ReassignTaskCheckAspect {

    @Autowired
    private ExtActTaskReassignMapper extActTaskReassignMapper;

    @Pointcut("@annotation(com.nbjtjc.safe.annotation.ReassignTaskCheck)")
    public void taskCheckPointCut() {
    }

    @Before("taskCheckPointCut()")
    public void before(JoinPoint point) {
        ActivitiParams activitiParams = (ActivitiParams) point.getArgs()[0];
        if (ObjectUtil.isNull(activitiParams) || ObjectUtil.isNull(activitiParams.getTaskId())) {
            throw new ValidateException("任务验证时，参数不能为空");
        }
        ExtActTaskReassign extActTaskReassign = new ExtActTaskReassign();
        extActTaskReassign.setTaskId(activitiParams.getTaskId());
        extActTaskReassign.setHasReturn(false);
        List<ExtActTaskReassign> extActTaskReassignList =
                extActTaskReassignMapper.select(extActTaskReassign);
        if(CollectionUtil.isNotEmpty(extActTaskReassignList)){
            throw new ValidateException("该任务是改派状态，先原路返回");
        }
    }
}
